package com.llsydn.beijingnews.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author lilinshen
 * @title 本地缓存工具类
 * @description 请填写相关描述
 * @date 2018/11/5 15:10
 */
public class LocalCacheUtils {
    private final MemoryCacheUtils memoryCacheUtils;
    private final Context context;

    public LocalCacheUtils(MemoryCacheUtils memoryCacheUtils, Context context) {
        this.memoryCacheUtils = memoryCacheUtils;
        this.context = context;
    }

    /**
     * 根据Url获取图片
     *
     * @param imageUrl
     * @return
     */
    public Bitmap getBitmapFromUrl(String imageUrl) {
        //判断sdcard是否挂载
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            try {
                //xxxxxx
                String fileName = Md5Utls.encode(imageUrl);
                //得到sd卡下的files路径 /storage/emulated/0/Android/data/com.llsydn.beijingnews/files/images
                String filePath = context.getExternalFilesDir(null).getAbsolutePath() + "/images";
                File file = new File(filePath, fileName);
                LogUtil.e("本地路径：" + file.getPath());
                if (file.exists()) {
                    FileInputStream is = new FileInputStream(file);
                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                    if (bitmap != null) {
                        memoryCacheUtils.putBitmap(imageUrl, bitmap);
                    }
                    return bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.e("图片获取失败");
            }
        }
        return null;
    }

    /**
     * 根据Url保存图片
     *
     * @param imageUrl url
     * @param bitmap   图片
     */
    public void putBitmap(String imageUrl, Bitmap bitmap) {

        //判断sdcard是否挂载
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            try {
                //xxxxxx
                String fileName = Md5Utls.encode(imageUrl);
                //得到sd卡下的files路径 /storage/emulated/0/Android/data/com.llsydn.beijingnews/files/images
                String filePath = context.getExternalFilesDir(null).getAbsolutePath() + "/images";
                File file = new File(filePath, fileName);
                LogUtil.e("本地路径：" + file.getPath());
                File parentFile = file.getParentFile();
                if (!parentFile.exists()) {
                    //创建目录
                    parentFile.mkdirs();
                }
                if (!file.exists()) {
                    file.createNewFile();
                }
                //保存图片
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.e("图片本地缓存失败");
            }
        }
    }
}
